package com.unione.map.view.fragment

import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.View.GONE
import android.view.View.OnClickListener
import android.view.View.VISIBLE
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.Navigation
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.impl.LoadingPopupView
import com.unione.map.R
import com.unione.map.RobotMapApplication
import com.unione.map.databinding.FragmentCreateMapStep1Binding
import com.unione.map.view.act.MainActivity
import com.unione.map.view.viewmodel.CreateMapStep1ViewModel
import com.unione.unione_base.view.BaseFragment
import com.unione.unione_protocol.robot_auto.bean.RobotInfo
import com.unione.unione_voice.constant.VoiceCommonConstant
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class CreateMapStep1Fragment : AppBaseFragment<FragmentCreateMapStep1Binding>(), OnClickListener {

    private val viewModel: CreateMapStep1ViewModel by viewModels()
    private lateinit var loadingPopupView: LoadingPopupView
    private var mLoadingPopup: BasePopupView? = null

    companion object {
        const val TAG = "CreateMapStep1Fragment"

    }

    override fun onCreateViewBinding(
        inflater: LayoutInflater,
        parent: ViewGroup?
    ): FragmentCreateMapStep1Binding {
        return FragmentCreateMapStep1Binding.inflate(inflater, parent, false)
    }

    override fun init() {
        initView()
        initListeners()
    }


    private fun initView() {
        lifecycleScope.launch(Dispatchers.IO) {
            while (true) {
                if (!this@CreateMapStep1Fragment::loadingPopupView.isInitialized) {
                    lifecycleScope.launch(Dispatchers.Main) {
                        loadingPopupView = LoadingPopupView(requireContext(), 0).apply {
                            setStyle(LoadingPopupView.Style.Spinner)
                        }
                        mLoadingPopup =
                            XPopup.Builder(requireContext()).asCustom(loadingPopupView).show()
                    }
                }
                if ((requireActivity() as MainActivity).mRobotInfo != null) {
                    lifecycleScope.launch(Dispatchers.Main) {
                        mLoadingPopup?.dismiss()
                    }
                    break
                }
                delay(3000)
            }
        }
    }

    private fun showUnableTipView(tip: String) {
        lifecycleScope.launch(Dispatchers.Main) {
            binding.clTip.visibility = GONE
            binding.tvTip.visibility = VISIBLE
            binding.tvTip.text = tip
        }
    }

    private fun showEnableTipView() {
        lifecycleScope.launch(Dispatchers.Main) {
            binding.clTip.visibility = VISIBLE
            binding.tvTip.visibility = GONE
        }
    }


    private fun initListeners() {
        binding.clVoice.setOnClickListener(this)
        binding.clHome.setOnClickListener(this)
        binding.btnNext.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        if (v == null) return
        val nvController =
            Navigation.findNavController(
                requireActivity() as MainActivity,
                R.id.fragment_container_view
            )
        when (v.id) {
            R.id.btnNext -> {
                if ((requireActivity() as MainActivity).mRobotInfo == null) {
                    ToastUtils.showLong("暂时无法开启制图")
                    return
                }
                nvController.navigate(R.id.createMapStep2Fragment)
            }
        }
    }

}